﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Truextend.CoffeShop.Model;
using Truextend.CoffeShop.Service.Infrastructure;
using Truextend.CoffeShop.Bussiness.Interfaces;
using Truextend.CoffeShop.Bussiness;

namespace Truextend.CoffeShop.Service
{
    public class OrderService : BaseService
    {
        public IList<Order> GetOrdersForState(string state)
        {
            IList<Order> result = null;

            ExecuteManager(uow =>             
                            {
                                IOrderManager manager = new OrderManager(uow,null);
                                result = manager.GetOrdersForState(state);
                            });

            return result;            
        }

        public void ChangeStateOfAnOrder(Order order , string state) {

            ExecuteOrderManager(om => om.ChangeStateOfAnOrder(order, state));
        }

        public void AddOrder(Order order, IList<int> listProductsId)
        {
            //ExecuteManager(uow => 
            //                {
            //                    IDetailManager detailManager = new DetailManager(uow,new ProductManager(uow));
            //                    IOrderManager manager = new OrderManager(uow, detailManager);

            //                    manager.AddOrder(order, listProductsId);

            //                });
            ExecuteOrderManager(om=>om.AddOrder(order, listProductsId));
        }

        public Order GetOrderById(int orderId)
        {
            Order result = null;

            //ExecuteManager(uow => 
            //                {
            //                    IOrderManager manager = new OrderManager(uow,null);
            //                    result = manager.GetOrderById(orderId);
            //                });
            ExecuteOrderManager(om => result = om.GetOrderById(orderId));
            return result;            
        }

        public void CloseOrderReception(List<string> allEmails)
        {            
            ExecuteOrderManager(om=> om.CloseOrderReception(allEmails));
        }

        public void StartOrderReception(List<string> allEmails)
        {
            ExecuteOrderManager(om => om.StartOrderReception(allEmails));
        }

        public void DeleteOrder(Order order)
        {
            ExecuteOrderManager(om => om.DeleteOrder(order));
        }

        private void ExecuteOrderManager(Action<IOrderManager> coreMethod, Action<IOrderManager> postCommit = null)
        {
            OrderManager orderManager = null;
            ExecuteManager(uow =>
            {
                var productManager = new ProductManager(uow);
                var detaiManager = new DetailManager(uow, productManager);
                orderManager = new OrderManager(uow, detaiManager);

                coreMethod(orderManager);
            });
            if (postCommit != null) postCommit(orderManager);
        }        
        
    }
}
